
function showActive() {
    const l = document.getElementById('loading')
    if (l) {
        l.style.display = 'none'
    }
    const a = document.getElementById('active')
    if (a) {
        a.style.display = 'block'
    }
}
function removeAllSplash() {
    const loading = document.getElementById('loading');
    const active = document.getElementById('active');
    if (loading) {
        setTimeout(() => {
            loading.parentNode.removeChild(loading);
        }, 1000)

    }
    if (active) {
        active.parentNode.removeChild(active);
    }
}
function activeApp(content) {
    if (window.ry) {
        window.ry.subjectLicense(content, () => {
            if (!window.location.origin.startsWith('file://')) {
                return;
            }
            window.ry.startJsAction()
        }, () => { alert('激活失败！') })
    }
}
var activeBarcodeListener = {
    success: (result) => {
        let code = ''
        if (result.barcodeData.indexOf(' ') != -1) {
            code = result.barcodeData.replace(/ /g, '+')
        } else {
            code = result.barcodeData
        }
        activeApp(code)
        removeAllSplash();
        window.cordova.plugins.RyBridge.removeBarcodeEventListener(activeBarcodeListener)
    },
    error: (error) => {
        alert('识别二维码失败！' + error)
    }
}

document.addEventListener("deviceready", () => {
    window.ry = window.cordova.plugins.RyBridge
    if (window.ry) {
        console.log('rybridge is ready')
        // 等待barcode scanner ok
        window.ry.barcodeDeviceReady(() => {
            window.ry.checkLicense(() => {
                if (!window.location.origin.startsWith('file://')) {
                    removeAllSplash();
                    return;
                }
                console.log('license is ok')
                window.ry.startJsAction()
                removeAllSplash();

            }, () => {
                if (!window.location.origin.startsWith('file://')) {
                    removeAllSplash();
                    return;
                } else {
                    console.log('license is not ok')
                    showActive()
                }
                window.ry.addBarcodeEventListener(activeBarcodeListener)
            })
        }, () => {
            alert('设备初始化失败，无法启动应用！')
        })
    }
}, false);

